package avaliacao;

import avaliacao.Exception.Err;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.Normalizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.RandomStringUtils;
import sun.misc.BASE64Encoder;


public class Util {
    
    public static String verificarCamposAlfabeticos
            (String campo, int tamanhoMinimo, String caracteresValidos)
    {
        Pattern padrao = Pattern.compile(caracteresValidos);
        Matcher pesquisa;

        if(campo == null){
            return Err.BlankField;
        }

        if(campo.trim().length() < tamanhoMinimo){
            return Err.ShortLength;
        }

        pesquisa = padrao.matcher(campo);

        if(pesquisa.matches()){
            return Err.OK;
        }
        else{
            return Err.InvalidChar;
        }
    }

    public static String geraSenha()
    {
        /** Creates a random string whose length is the number of characters specified.
         * Characters will be chosen from the set of alpha-numeric characters.*/
        String senha = RandomStringUtils.randomAlphanumeric(15);
        return senha;
    }

    public static String encriptaSenha(String senha)
    {
        String novaSenha = senha;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] senhaCript = md.digest(senha.getBytes());
            BASE64Encoder codificador = new BASE64Encoder();
            novaSenha = codificador.encode(senhaCript);

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }

        return novaSenha;
    }

    public static final String removeDiacriticals(final String input) {
        if (input == null) {
            return input;
        }
        final String decomposed = Normalizer.normalize(input, Normalizer.Form.NFD);
        String final2 = decomposed.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
        return final2;
    }
}
